package com.aiyige.page.login.presenter;

import android.os.Handler;
import android.text.TextUtils;

import com.aiyige.MyApp;
import com.aiyige.R;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.eventbus.EventLogin;
import com.aiyige.model.User;
import com.aiyige.page.login.LoginPage;
import com.aiyige.page.login.callbacks.ILoginCallBack;
import com.aiyige.page.login.model.ILoginModel;
import com.aiyige.page.login.model.impl.LoginManager;
import com.aiyige.page.login.view.ILoginView;
import com.aiyige.utils.ToastX;
import com.alibaba.android.arouter.launcher.ARouter;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.vondear.rxtools.RxActivityTool;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by xiachao on 2017/8/15.
 */

public class LoginPresenter {
    private ILoginModel mUserModel;
    private ILoginView mUserView;
    private Handler mHandler = new Handler();

    /**
     * 把LoginActivity的需要用到的model通过ILoginView接口传递到Presenter来
     *
     * @param mUserView
     */
    public LoginPresenter(ILoginView mUserView) {
        this.mUserView = mUserView;
        this.mUserModel = new LoginManager();
        mUserModel.setView(mUserView);
    }

    public void login() {
        if (!checkInput()) {
            return;
        }
        mUserModel.login(mUserView.getUserName(), mUserView.getPassword(), new ILoginCallBack() {
            @Override
            public void success(User userBean) {
                ToastX.show(MyApp.getInstance().getString(R.string.login_success));
                //SessionUtil.updateCurrentSession(userBean);
                EventBus.getDefault().post(new EventLogin(userBean));
                RxActivityTool.finishActivity(LoginPage.class);
                if (!TextUtils.isEmpty(mUserView.getTargetPage())) {
                    ARouter.getInstance()
                            .build(mUserView.getTargetPage())
                            .with(mUserView.getTargetData())
                            .navigation();
                }
            }

            @Override
            public void error(String errorCode) {
                if (!TextUtils.isEmpty(errorCode))
                    ToastX.show(errorCode);
            }

        });
    }

    private boolean checkInput() {

        if (TextUtils.isEmpty(mUserView.getUserName())) {
            ToastX.show(MyApp.getInstance().getString(R.string.username_empty));
            return false;
        }
        if (TextUtils.isEmpty(mUserView.getPassword())) {
            ToastX.show(MyApp.getInstance().getString(R.string.password_empty));
            return false;
        }
        return true;
    }

    public void loginWeChat(BaseActivity activity) {
        mUserModel.loginByOther(activity, SHARE_MEDIA.WEIXIN);
    }

    public void loginQQ(BaseActivity activity) {
        mUserModel.loginByOther(activity, SHARE_MEDIA.QQ);
    }

    public void loginWeibo(BaseActivity activity) {
        mUserModel.loginByOther(activity, SHARE_MEDIA.SINA);
    }
}
